package com.zxw.common.core.base.res;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.zxw.common.core.base.enums.ResponseEnum;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class BaseRes<T> implements Serializable {

    private static final long serialVersionUID = -2700441682557255197L;

    private Integer code = 200;
    private long total;
    private String msg;
    private String token_type;
    private String access_token;
    private T data;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setMsg(ResponseEnum responseEnum) {
        this.msg = responseEnum.getMsg();
    }

    public static BaseRes ok() {
        BaseRes baseRes = new BaseRes();
        return baseRes;
    }

    public static BaseRes okSend() {
        BaseRes baseRes = new BaseRes();
        baseRes.setMsg(ResponseEnum.OK_SEND);
        return baseRes;
    }

    public static BaseRes okAdd() {
        BaseRes baseRes = new BaseRes();
        baseRes.setMsg(ResponseEnum.OK_ADD);
        return baseRes;
    }

    public static <T> BaseRes okAdd(T obj) {
        BaseRes baseRes = new BaseRes();
        baseRes.setMsg(ResponseEnum.OK_ADD);
        baseRes.setData(obj);
        return baseRes;
    }


    public static BaseRes okUpdate() {
        BaseRes baseRes = new BaseRes();
        baseRes.setMsg(ResponseEnum.OK_UPDATE);
        return baseRes;
    }


    public static BaseRes okDelete() {
        BaseRes baseRes = new BaseRes();
        baseRes.setMsg(ResponseEnum.OK_DELETE);
        return baseRes;
    }

    public static <T> BaseRes<T> ok(T obj) {
        BaseRes baseRes = new BaseRes();
        baseRes.setData(obj);
        return baseRes;
    }

    public static <T> BaseRes<IPage<T>> okPage(IPage<T> obj) {
        BaseRes baseRes = new BaseRes();
        baseRes.setData(obj.getRecords());
        baseRes.setTotal(obj.getTotal());
        baseRes.setMsg(ResponseEnum.OK);
        return baseRes;
    }


    public static <T> BaseRes<T> oKToken(T obj, String token_type, String access_token) {
        BaseRes baseRes = new BaseRes();
        baseRes.setData(obj);
        baseRes.setToken_type(token_type);
        baseRes.setAccess_token(access_token);
        return baseRes;
    }

    public static <T> BaseRes<T> err(int code, String msg) {
        BaseRes baseRes = new BaseRes();
        baseRes.setCode(code);
        baseRes.setMsg(msg);
        return baseRes;
    }

    public static <T> BaseRes<T> err(ResponseEnum responseEnum) {
        BaseRes baseRes = new BaseRes();
        baseRes.setCode(responseEnum.getCode());
        baseRes.setMsg(responseEnum.getMsg());
        return baseRes;
    }

    public static <T> BaseRes<T> err(ResponseEnum responseEnum, String msg) {
        BaseRes baseRes = new BaseRes();
        baseRes.setCode(responseEnum.getCode());
        baseRes.setMsg(responseEnum.getMsg() + msg);
        return baseRes;
    }

    public static <T> BaseRes<T> err(String msg) {
        BaseRes baseRes = new BaseRes();
        baseRes.setCode(ResponseEnum.NORMAL_ERROR.getCode());
        baseRes.setMsg(msg);
        return baseRes;
    }

}
